package com.hkyp.ss.course.web.controller;

import com.hkyp.ss.course.apis.UserService;
import com.hkyp.ss.course.apis.cons.CommonConstants;
import com.hkyp.ss.course.apis.dto.request.LoginDTO;
import com.hkyp.ss.course.apis.dto.request.ResetPwdDTO;
import com.hkyp.ss.course.apis.exception.UnacceptableException;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.apis.utils.DataCheckUtil;
import com.hkyp.ss.course.web.dto.base.ResultBean;
import com.hkyp.ss.course.web.dto.request.WebLoginDTO;
import com.hkyp.ss.course.web.dto.request.WebResetPwdDTO;
import com.hkyp.ss.course.web.utils.IPUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(description = "登录/注册 等")
@Slf4j
@RestController
@RequestMapping(value = "/a/h/")
public class LoginController{

    @Reference(version = "1.0")
    UserService userService;


    /**
     * 用户登录
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultBean login(@Valid @RequestBody WebLoginDTO loginDTO, HttpServletResponse response)throws Exception{

        loginDTO.setIp(IPUtil.getIpAddr());
        String token = userService.doLogin(BeanMapper.map(loginDTO, LoginDTO.class));
        Map map = new HashMap<>();
        map.put("token",token);
        return new ResultBean<>(map);

    }

    @ApiOperation(value = "注册-发送注册邮件")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResultBean Register(@Valid @RequestBody WebLoginDTO loginDTO, HttpServletRequest request)throws Exception{

        loginDTO.setIp(IPUtil.getIpAddr());
        userService.doRegister(BeanMapper.map(loginDTO, LoginDTO.class), CommonConstants.WEB_REQUEST_URL); // IPUtil.getBasePath(request)

        return new ResultBean();
    }

    @ApiOperation(value = "注册-邮箱激活")
    @RequestMapping(value = "/email/activation", method = RequestMethod.GET)
    public ResultBean emailActivation(HttpServletRequest request, String token)throws Exception{
        if(StringUtils.isEmpty(token)){
            throw new UnacceptableException("token不能为空");
        }

        userService.activeEmail(token);
        return new ResultBean();
    }

    /**
     * 重置密码-发送激活链接
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "重置密码-发送重置密码页面链接")
    @RequestMapping(value = "/user/pwd/pre", method = RequestMethod.POST)
    public ResultBean resetPwdPre(@RequestBody WebResetPwdDTO webModifyEmailPreDTO, HttpServletRequest request){

        DataCheckUtil.isMail(webModifyEmailPreDTO.getEmail());

        ResetPwdDTO resetPwdDTO = new ResetPwdDTO();
        resetPwdDTO.setBasePath(CommonConstants.WEB_REQUEST_URL);
        resetPwdDTO.setEmail(webModifyEmailPreDTO.getEmail());
        userService.reSetPwdSendEmailPre(resetPwdDTO);
        return new ResultBean();

    }



}
